<?php

namespace app\adminabc\controller;

use app\adminabc\controller\Base;

class User extends Base {
	public function index() {
		$cid = $this->request->param ['id'];
		$list = model( 'User' )->getUser ();
		$this->assign ( 'list', $list );
		return $this->fetch ( 'list-user' );
	}
	function add() {
		if ($this->request->isPost()) {
			$account = $this->request->param ( 'account' );
			$pwd = $this->request->param ('pwd');
			$info = model( 'User' )->getInfo ( array (['u.account' ,'=', $account ]) );
			if ($info ['id'] > 0)
				$this->error ( '该账号已存在' );
			if ($pwd == '')
				$this->error ( '密码不能为空' );
			$data ['account'] = $this->request->param ( 'account' );
			$data ['password'] = md5 ( $this->request->param ('pwd') );
			$data ['nickname'] = $this->request->param ( 'nickname' );
			$data ['type_id'] = 1;
			$data ['status'] = $this->request->param ( 'status' ) == '' ? 1 : $this->request->param ( 'status' );
			$data ['create_time'] = time();
			$data ['update_time'] = time();
			$log = model( 'User' )->addUser ( $data );
			if ($log > 0) {
				unset ( $data );
				$data ['role_id'] = $this->request->param ( 'role_id' );
				$data ['user_id'] = $log;
				model( 'User' )->addRoleUser ( $data );
				$this->success ( '添加成功' );
			} else
				$this->error ( '添加失败' );
		} else {
			$this->assign ( 'html_role', $this->html_role () );
			return $this->fetch ( 'add' );
		}
	}
	function edit() {
		$id = $this->request->param ( 'id' );
		$info = model( 'User' )->getInfo ( array (['u.id' ,'=', $id ]) );
		if ($this->request->isPost()) {
			$pwd = $this->request->param ('pwd');
			if ($pwd != '')
				$data ['password'] = md5 ( $this->request->param ('pwd') );
			$data ['nickname'] = $this->request->param ( 'nickname' );
			$data ['status'] = $this->request->param ( 'status' ) == '' ? 1 : $this->request->param ( 'status' );
			$log1 = model( 'User' )->editUser ( $id, $data );
			unset ( $data );
			$data ['role_id'] = $this->request->param ('role_id');
			$data ['update_time'] = time();
			$log2 = model( 'User' )->editRoleUser ( $info ['id'], $data );
			
			if (($log1 + $log2) > 0)
				$this->success ( '修改成功' );
			else
				$this->error ( '修改失败' );
		} else {
			$this->assign ( 'info', $info );
			$this->assign ( 'html_role', $this->html_role ( $info ['role_id'] ) );
			return $this->fetch ( 'add' );
		}
	}
	function del() {
		$id = $this->request->param ( 'id' );
		$info = model( 'User' )->getInfo ( array (['u.id' ,'=', $id ]) );
		if ($info ['account'] == 'admin')
			$this->error ( '系统管理员禁止删除' );
		$log = model( 'User' )->delUser ( $id );
		if ($log > 0)
			$this->success ( '删除成功' );
		else
			$this->error ( '删除失败' );
	}
	function html_role($sel) {
		$html = "<select name='role_id'>";
		$role = model( 'Role' )->getRole ();
		foreach ( $role as $k => $v )
			if ($v ['id'] == $sel)
				$html .= sprintf ( "<option value='%s'  selected='selected'>%s</option>", $v ['id'], $v ['name'] );
			else
				$html .= sprintf ( "<option value='%s'>%s</option>", $v ['id'], $v ['name'] );
		$html .= "</select>";
		return $html;
	}
}